package cn.mw.cmdb.viewProcessor;

import cn.mw.cmdb.entity.InstanceInfo;
import cn.mw.cmdb.entity.ModelInfo;
import cn.mw.cmdb.processor.TableDataProcessorContext;
import cn.mw.cmdb.service.impl.CmdbServiceManage;
import cn.mw.cmdb.service.impl.CustomModelServiceImpl;
import cn.mw.cmdb.util.CMDBTool;
import cn.mw.cmdb.view.StaticsViewInstance;
import cn.mw.components.mongodb.entity.CriteriaOpsType;
import cn.mw.components.mongodb.entity.SearchParam;
import cn.mw.components.mongodb.entity.SubCondition;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class ModelViewProcessor implements RelateDataViewProcessor<InstanceInfo, StaticsViewInstance>{
    private static final String ModelKey = "model";

    private ViewProcessorManage viewProcessorManage;

    public ModelViewProcessor(ViewProcessorManage viewProcessorManage){
        this.viewProcessorManage = viewProcessorManage;
    }

    @Override
    public void init(InstanceInfo instanceInfo, StaticsViewInstance staticsViewInstance) {
        String modelId = instanceInfo.getModelId();
        if(StringUtils.hasText(modelId)){
            viewProcessorManage.addFieldIdSet(ModelKey ,modelId);
            staticsViewInstance.setModelName(modelId);
        }
    }

    @Override
    public void selectRelateData(TableDataProcessorContext context) throws Exception {
        CmdbServiceManage cmdbServiceManage = (CmdbServiceManage)context.get(CmdbServiceManage.class.getName());
        CustomModelServiceImpl modelService = (CustomModelServiceImpl)cmdbServiceManage.getModelService(CustomModelServiceImpl.ID);
        Set<String> modelIdSet = viewProcessorManage.getFieldIdSet(ModelKey);
        if(null != modelIdSet && modelIdSet.size() > 0){
            List<String> modelIds = modelIdSet.stream().collect(Collectors.toList());
            SearchParam searchParam = new SearchParam();
            SubCondition subCondition = new SubCondition(CriteriaOpsType.in.name(), modelIds);
            searchParam.addSubCondition(CMDBTool.getFieldName(ModelInfo::getId) ,subCondition);
            List<ModelInfo> list = modelService.selectList(searchParam);
            if(null != list){
                for(ModelInfo modelInfo: list){
                    viewProcessorManage.addFieldIdMap(ModelKey ,modelInfo.getId() ,modelInfo.getModelName());
                }
            }
        }
    }

    @Override
    public void setRelateData(StaticsViewInstance staticsViewInstance) {
        String id = staticsViewInstance.getModelName();
        Map<String, String> map = viewProcessorManage.getFieldIdMap(ModelKey);
        if(StringUtils.hasText(id) && null != map){
            String name = map.get(id);
            if(StringUtils.hasText(name)){
                staticsViewInstance.setModelName(name);
            }
        }
    }

    @Override
    public int hashCode() {
        return getClass().getSimpleName().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return getClass().getSimpleName().equals(obj.getClass().getSimpleName());
    }
}
